'use client'

import { useEffect, useState } from "react";
import { BurnDownInfo, EntryInfo, IssueInfo, ShareInfo, SpritInfo, SUMMARY_STATE, SummaryItemInfo } from "@/grpc/types";
import { Card, Descriptions } from "antd";
import moment from "moment";
import { IssueList } from "./issue/IssueList";
import { GanttPanel } from "./workPlan/GanttPanel";
import { BurnDownPanel } from "./workPlan/BurnDownPanel";
import { SummaryPanel } from "./workPlan/SummaryPanel";

export interface WorkPlanPanelProps {
    shareInfo: ShareInfo;
}

export default function WorkPlanPanel(props: WorkPlanPanelProps) {
    const [entryInfo, setEntryInfo] = useState<EntryInfo | null>(null);
    const [entryErrorMsg, setEntryErrorMsg] = useState("");
    const [spritInfo, setSpritInfo] = useState<SpritInfo | null>(null);
    const [spritErrorMsg, setSpritErrorMsg] = useState("");
    const [issueInfoList, setIssueInfoList] = useState<IssueInfo[]>([]);
    const [issueListErrorMsg, setIssueListErrorMsg] = useState("");
    const [burnDownInfoList, setBurnDownInfoList] = useState<BurnDownInfo[]>([]);
    const [burnDownErrorMsg, setBurnDownErrorMsg] = useState("");
    const [summaryItemList, setSummaryItemList] = useState<SummaryItemInfo[]>([]);
    const [summaryErrorMsg, setSummaryErrorMsg] = useState("");

    const loadSpritInfo = async () => {
        const res = await fetch(`/api/workplan/info?id=${props.shareInfo.shareId}&secret=${props.shareInfo.shareSecret}`);
        const resData = await res.json() as {
            code: number;
            errMsg: string;
            info: SpritInfo;
        };
        if (resData.code == 0) {
            setSpritInfo(resData.info);
        } else {
            setSpritErrorMsg(resData.errMsg);
        }
    };

    const loadEntryInfo = async () => {
        const res = await fetch(`/api/entry?id=${props.shareInfo.shareId}&secret=${props.shareInfo.shareSecret}`);
        const resData = await res.json() as {
            code: number;
            errMsg: string;
            entry: EntryInfo;
        };
        if (resData.code == 0) {
            setEntryInfo(resData.entry);
        } else {
            setEntryErrorMsg(resData.errMsg);
        }
    };

    const loadIssueInfoList = async () => {
        const res = await fetch(`/api/issue/list?id=${props.shareInfo.shareId}&secret=${props.shareInfo.shareSecret}`);
        const resData = await res.json() as {
            code: number;
            errMsg: string;
            infoList: IssueInfo[];
        };
        if (resData.code == 0) {
            setIssueInfoList(resData.infoList);
        } else {
            setIssueListErrorMsg(resData.errMsg);
        }
    };

    const loadBurnDownInfoList = async () => {
        const res = await fetch(`/api/workplan/listBurnDown?id=${props.shareInfo.shareId}&secret=${props.shareInfo.shareSecret}`);
        const resData = await res.json() as {
            code: number;
            errMsg: string;
            infoList: BurnDownInfo[];
        };
        if (resData.code == 0) {
            setBurnDownInfoList(resData.infoList);
            console.log(resData);
        } else {
            setBurnDownErrorMsg(resData.errMsg);
        }
    };

    const loadSummaryItemList = async () => {
        const res = await fetch(`/api/workplan/listSummary?id=${props.shareInfo.shareId}&secret=${props.shareInfo.shareSecret}`);
        const resData = await res.json() as {
            code: number;
            errMsg: string;
            itemList: SummaryItemInfo[]
        };
        if (resData.code == 0) {
            setSummaryItemList(resData.itemList);
        } else {
            setSummaryErrorMsg(resData.errMsg);
        }
    };

    useEffect(() => {
        loadSpritInfo();
        loadEntryInfo();
        loadIssueInfoList();
        loadBurnDownInfoList();
        loadSummaryItemList();
    }, []);

    return (
        <>
            {spritInfo == null && spritErrorMsg != "" && spritErrorMsg}
            {entryInfo == null && entryErrorMsg != "" && entryErrorMsg}
            {issueListErrorMsg != "" && issueListErrorMsg}
            {burnDownErrorMsg != "" && burnDownErrorMsg}
            {summaryErrorMsg != "" && summaryErrorMsg}

            {spritInfo != null && entryInfo != null && (
                <Card title={entryInfo.entryTitle ?? ""} headStyle={{ backgroundColor: "#eee" }}>
                    <Descriptions bordered>
                        <Descriptions.Item label="开始时间">{moment(entryInfo.extraSpritInfo?.startTime ?? 0).format("YYYY-MM-DD")}</Descriptions.Item>
                        <Descriptions.Item label="结束时间">{moment(entryInfo.extraSpritInfo?.endTime ?? 0).format("YYYY-MM-DD")}</Descriptions.Item>
                        <Descriptions.Item label="创建用户">{entryInfo.createDisplayName}</Descriptions.Item>
                        <Descriptions.Item label="任务数量">{spritInfo.taskCount}</Descriptions.Item>
                        <Descriptions.Item label="缺陷数量">{spritInfo.bugCount}</Descriptions.Item>
                        <Descriptions.Item label="总结阶段">{spritInfo.summaryState == SUMMARY_STATE.SUMMARY_COLLECT ? "收集阶段" : "汇总阶段"}</Descriptions.Item>
                    </Descriptions>
                </Card>
            )}

            <Card title="任务/缺陷列表" headStyle={{ backgroundColor: "#eee" }}>
                <IssueList issueInfoList={issueInfoList} />
            </Card>
            {spritInfo != null && entryInfo != null && issueInfoList.length > 0 && (
                <GanttPanel spritName={entryInfo.entryTitle ?? ""} startTime={entryInfo.extraSpritInfo?.startTime ?? 0} endTime={entryInfo.extraSpritInfo?.endTime ?? 0} issueInfoList={issueInfoList} />
            )}

            {entryInfo != null && entryInfo.extraSpritInfo != undefined && (
                <BurnDownPanel spritInfo={entryInfo.extraSpritInfo} burnDownInfoList={burnDownInfoList} />
            )}
            {entryInfo != null && (
                <SummaryPanel summaryItemList={summaryItemList} />
            )}
        </>
    );
}